﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iworld.Administrator.Service.Export;
using Iworld.Administrator.Service.Import;
using Iworld.basis.Service;
using Iworld.Administrator.Manager;
using Iworld.Main.Impl;
using Iworld.Author.Model;

namespace Iworld.Administrator.Service
{
    /// <summary>
    /// 管理员相关的数据服务
    /// </summary>
    public class AdministratorService : IAdministratorService
    {

        #region 查询数据
        public NormalResult<KeepHeartbeatExport> KeepHeartbeat(KeepHeartbeatImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    AdministratorLoginInfoPond.KeepHeartbeat(import.Token);
                    int countOfWithdrawals = 0;
                    if(import.Self.Group.CanReviewWithdrawals)
                    {
                      countOfWithdrawals= db.WithdrawalsRecords.Count(x => x.Status == WithdrawalsStatus.处理中);
                    }
                    DateTime limitTime = DateTime.Now.AddSeconds(-15);
                    int countOfUsersInClient = db.AuthorLoginInfos.Count(x => x.Hit == false && x.LastHeartbeatTime > limitTime);
                    int countOfUsersInBackstage = db.AdministratorLoginInfos.Count(x => x.Hit == false && x.LastHeartbeatTime > limitTime);
                    return new NormalResult<KeepHeartbeatExport>(new KeepHeartbeatExport() 
                    { 
                        CountOfUsersInBackstage = countOfUsersInBackstage, 
                        CountOfUsersInClient = countOfUsersInClient,
                        CountOfWithdrawals = countOfWithdrawals
                    }); 
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<KeepHeartbeatExport>(null,ex.Message);
            }
        }

        /// <summary>
        /// 获取管理员信息的分页列表
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回管理员信息的分页列表</returns>
        public PageResult<AdministratorExport> GetAdministrators(GetAdministratorsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetAdministrators(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<AdministratorExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取管理员用户组的分页列表
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回管理员用户组的分页列表</returns>
        public PageResult<AdministratorGroupExport> GetAdministratorGroups(GetAdministratorGroupsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetAdministratorGroups(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<AdministratorGroupExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取管理员登陆记录的分页列表
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回管理员登陆记录的分页列表</returns>
        public PageResult<AdministratorLandingRecordExport> GetAdministratorLandingRecords(GetAdministratorLandingRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetAdministratorLandingRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<AdministratorLandingRecordExport>(ex.Message);
            }
        }
        #endregion

        #region 操作

       

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回操作结果</returns>
        public NormalResult<AdministratorExport> Login(LoginImport import)
        {
            try
            {
              using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    Iworld.Administrator.Model.Administrator administrator = new AdministratorManager(db).Login(import);
                    string token = AdministratorLoginInfoPond.AddInfo(administrator.Id);
                    AdministratorExport ae = new AdministratorExport(administrator, token);
                    return new NormalResult<AdministratorExport>(ae);
                } 
            }
            catch (Exception ex)
            {
                return new NormalResult<AdministratorExport>(null, ex.Message);
            }
        }

        /// <summary>
        /// 创建管理员
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult CreateAdministrator(CreateAdministratorImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorManager(db).Create(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 编辑管理员
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditAdministrator(EditAdministratorImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 修改自身密码
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditPassword(EditPasswordImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 移除管理员
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult RemoveAdministrator(RemoveAdministratorImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorManager(db).Remove(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 创建管理员组
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>

        public NormalResult CreateAdministratorGroup(CreateAdministratorGroupImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorGroupManager(db).Create(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 编辑管理员组
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditAdministratorGroup(EditAdministratorGroupImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorGroupManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 移除管理员组
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult RemoveAdministratorGroup(RemoveAdministratorGroupImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AdministratorGroupManager(db).Remove(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }
       
        #endregion



       
    }
}
